/*
 * cxxomfort example: cxxo_foreach
 * 
 * This program provides sample usage of the 
 * "foreach loop" emulation feature.
 * 
 * 
 */

#include <cxxomfort/base.hpp>
#include <cxxomfort/string_view.hpp>
#include <cxxomfort/cxxo-utils.hpp> // foreach
#include <list>
#include <iostream>

std::list<char> obj( 3, 'o' );

std::list<char> & get_obj() {
   std::cout << "here!\n";
   return obj;
}

std::list<char> const & get_obj_const() {
   std::cout << "here!const!\n";
   return obj;
}

std::list<char>& getlist() CXXO_NOEXCEPTNOTHROW {
	return obj;
}

int main (int argc, char** argv) {
	(void)argc;
	using namespace std;
	cxxomfort::output_info(stdout);
	cout<< endl;
	cout<< "foreach status: "
	<< CXXO_COMPILER_SUPPORT_foreach<< " impl "
	<< CXXOMFORT_IMPLEMENTS_foreach<< " "
	<< "\n"<< endl;
	string_view const a0 = argv[0];
	cout<< "foreach in lvalue list:"<< endl;
	CXXO_FOREACH(char const c, a0 ) {
		getlist().push_back(c);
		cout<< c;
	}
	getlist().push_back(' ');
	cout<< endl;

#if (CXXOMFORT_IMPLEMENTS_foreach >= CXXO_IMPLSTATUS_BACKPORT())
	cout<< "foreach in temporary:"<< endl;
	CXXO_FOREACH(char const c, string("Hola Mundo") ) {
		getlist().push_front(c);
		cout<< c;
	}
	cout<< endl;
#endif

	cout<< "foreach on current contents:"<< endl;
	CXXO_FOREACH(char const c, get_obj_const() ) {
		cout<< c;
	}
	cout<< endl;
	
	cout<< "foreach writing on writable object:"<< endl;
	CXXO_FOREACH(char& c, get_obj() ) {
		cout<< c;
		++c;
	}
	cout<< endl;

	cout<< "foreach on view of object:"<< endl;
	CXXO_FOREACH(char c, get_obj() ) {
		cout<< c;
	}
	cout<< endl;
	
}
